/*
Action:通过操作mutation间接更新state的多个方法的对象
 */

// 注意要引入api接口函数
import {
  reqLogList,
  reqReadFile,
  logListen
} from '../api'

import {
  RECEIVE_LOADING,
  RECEIVE_NEXT,
  RECEIVE_UFILE_LOG_LIST,
  RECEIVE_PAGE_LAST_FILE,
  RECEIVE_READ_FILE,
  RECEIVE_WEBSOCKET
} from './mutation-types'

export default {
  async reqLogList ({commit}, parms) {
    const isLoading = true
    commit(RECEIVE_LOADING, {isLoading})

    const result = await reqLogList({parms})
    if (result != null && result.code === 0) {
      const isLoading = false
      commit(RECEIVE_LOADING, {isLoading})

      const log_list = result.data
      commit(RECEIVE_UFILE_LOG_LIST, {log_list})

      const isNext = result.next
      commit(RECEIVE_NEXT, {isNext})

      let page_last_file = ''
      if (log_list.length > 0) {
        const item = log_list[log_list.length - 1]
        page_last_file = item.FileName
      }
      commit(RECEIVE_PAGE_LAST_FILE, {page_last_file})
    } else {
      const isLoading = false
      commit(RECEIVE_LOADING, {isLoading})
      const isNext = true
      commit(RECEIVE_NEXT, {isNext})
    }
  },

  update_page_last_file ({commit}, page_last_file) {
    commit(RECEIVE_PAGE_LAST_FILE, {page_last_file})
  },

  clear_read_file ({commit}) {
    const read_file = ''
    commit(RECEIVE_READ_FILE, {read_file})
  },

  async reqReadFile ({commit}, parms) {
    const isLoading = true
    commit(RECEIVE_LOADING, {isLoading})
    const result = await reqReadFile({parms})
    if (result != null && result.code === 0) {
      const isLoading = false
      commit(RECEIVE_LOADING, {isLoading})

      const read_file = result.data
      commit(RECEIVE_READ_FILE, {read_file})
    } else {
      const isLoading = false
      commit(RECEIVE_LOADING, {isLoading})
    }
  },

  logListen ({commit}, web_socket) {
    commit(RECEIVE_WEBSOCKET, {web_socket})
  }
}
